import { tt } from "../../ttapi_interface/ttapi.js";
import * as tt2 from "../../ttapi_layer2/ttlayer2.js";
import { EUI_Style } from "./EUI_Style.js";


export class EUI_TabItem {
    name: string;
    index: number;
    modify: boolean;
    canclose: boolean;
    OnCloseAsync: ((name: string, modify: boolean) => Promise<boolean>) | null;
}
//Tab  Bar 是一个横向的控件，他有若干的项目组成，每个项目可以被选中和关闭
//
export class EUI_TabBar extends tt2.QUI_Container {
    constructor() {
        super();


        this.addChild(EUI_Style.CreateDefaultBack());
        this.uiscroll = new tt2.QUI_Panel_Scroll();
        this.uiscroll.getBorder().XLeft = 0;

        this.uiscroll.getBorder().XRight = 0;

        this.uiscroll.getBorder().YTop = 0;

        this.uiscroll.getBorder().YBottom = 0;


        this.uiscroll.dragout = false;
        this.uiscroll.dragDirection = tt2.QUI_DragDriection.LeftToRight;
        this.uiscroll.localRect.setHPosFill(17, 17);
        this.uiscroll.localRect.setVPosFill(1, 1);
        this.addChild(this.uiscroll);

        // for (var i = 0; i < 10; i++)
        //     this.AddItem("Default", i > 0);

        let leftPage = EUI_Style.CreateButton("<");
        leftPage.localRect.setHPosByLeftBorder(16, 1);
        leftPage.localRect.setVPosFill(1, 1);
        this.addChild(leftPage);
        leftPage.OnClick = () => {
            this.uiscroll.Scroll(this.uiscroll.getWorldRect().Width, 0);
        }

        let rightPage = EUI_Style.CreateButton(">");
        rightPage.localRect.setHPosByRightBorder(16, 1);
        rightPage.localRect.setVPosFill(1, 1);
        this.addChild(rightPage);
        rightPage.OnClick = () => {
            this.uiscroll.Scroll(-this.uiscroll.getWorldRect().Width, 0);
        }

    }
    items: EUI_TabItem[] = [];
    select: EUI_TabItem;
    OnSelect?: (name: string, index: number) => void;
    private uiscroll: tt2.QUI_Panel_Scroll
    private itemwidth: number = 128;
    AddItem(name: string, canclose: boolean = false, onClose: ((name: string) => Promise<boolean>) | null = null): EUI_TabItem {
        let item: EUI_TabItem = { name: name, index: this.items.length, modify: false, canclose: canclose, OnCloseAsync: onClose }
        this.items.push(item);
        this.UpdateItemUIAt(item.index);
        this.uiscroll.panelWidth = this.itemwidth * this.items.length;
        return item;
    }
    FindItem(name: string, startindex: number = 0): EUI_TabItem | null {
        for (var i = startindex; i < this.items.length; i++) {
            if (this.items[i].name == name) {
                return this.items[i];
            }
        }
        return null;
    }
    SelectItemAt(index: number) {
        let item = this.items[index];
        let oldselect = this.select;
        if (item != oldselect) {
            this.select = item;
            if (oldselect != null) {
                this.UpdateItemUIAt(oldselect.index);
            }
            if (this.select != null) {
                this.UpdateItemUIAt(this.select.index)

            }
        }
        if (this.select != null) {

            if (this.OnSelect != null) {
                this.OnSelect(item.name, item.index);
            }
        }
    }
    UpdateItemUIAt(index: number): void {
        let item = this.items[index];
        item.index = index;
        let select = this.select == item;
        while (index >= this.uiscroll.getChildCount()) {
            let uic = new tt2.QUI_Container();
            (uic as any).item = item;
            uic.localRect.setVPosByTopBorder(16);
            uic.localRect.setHPosByLeftBorder(this.itemwidth, index * this.itemwidth);

            let label = EUI_Style.CreateLabel(item.name);
            label.halign = tt2.QUI_HAlign.Left;
            label.localRect.setHPosFill(0, 0);
            label.cut = true;

            let selectbtn = new tt2.QUI_Overlay();
            selectbtn.localRect.setHPosFill();
            selectbtn.localRect.setVPosFill();
            selectbtn.OnPress = () => {
                let item = (uic as any).item as EUI_TabItem;
                this.SelectItemAt(item.index);
            }

            let cbtn = EUI_Style.CreateButton("X");
            cbtn.localRect.setVPosFill();
            cbtn.localRect.setHPosByRightBorder(16);
            cbtn.OnClick = async () => {
                let item = (uic as any).item as EUI_TabItem;
                await this.CloseItemAtAsync(item.index);
            }
            let back = EUI_Style.CreateDefaultBack()
            back.localRect.setVPosFill();
            back.localRect.setHPosFill();

            uic.addChild(back);
            uic.addChild(label);
            uic.addChild(selectbtn);
            uic.addChild(cbtn);
            this.uiscroll.addChild(uic);
        }
        let uic = this.uiscroll.getChild(index);
        let label = uic.getChild(1) as tt2.QUI_Label;
        let btnclose = uic.getChild(3) as tt2.QUI_Label;
        label.text = item.name;
        if (select) {
            label.color = new tt.Color(0, 0, 1, 1);
        }
        else {
            label.color = tt.Color.White;
        }
        if (item.modify) {
            if (label.text[0] != "*")
                label.text = "*" + label.text;
        }
        else {
            if (label.text[0] == "*")
                label.text = label.text.substring(1);
        }

        btnclose.Enable = item.canclose;



    }
    async CloseItemAtAsync(index: number): Promise<boolean> {
        let item = this.items[index];

        if (item.OnCloseAsync != null) {
            let cancel = await item.OnCloseAsync(item.name, item.modify);
            if (cancel)
                return true;
        }

        if (this.select != null && this.select.index == index) {
            this.select = null;
        }
        this.items.splice(index, 1);
        this.uiscroll.removeChildAt(index);

        for (var i = index; i < this.items.length; i++) {
            this.items[i].index = i;
            this.uiscroll.getChild(i).localRect.setHPosByLeftBorder(this.itemwidth, i * this.itemwidth);
        }
        this.uiscroll.panelWidth = this.itemwidth * this.items.length;
        this.uiscroll.Scroll(0, 0);
        return false;
    }

}